package com.cvit.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.model.Manager;
import com.cvit.utils.Msg;
import com.cvit.service.ManagerRoleService;
import com.cvit.service.ManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class ManagerConfigController {

	/**
	 * @author WYHY
	 */
	@Autowired
	ManagerService managerService;
	@Autowired 
	ManagerRoleService managerRoleService;
	
	/**
	 * @author WYHY
	 * @param 菜单
	 * */
	/*@RequestMapping("/selmenuM")
	@ResponseBody
	public List menuM(@RequestParam(value="managerid")String managerid) {
		List list=permissionService.selMenuM(managerid);
		return list;
	}*/
	
	/**
	 * @author WYHY
	 * @param 添加管理员
	 * */
	@RequestMapping("/addMana")
	@ResponseBody
	public Msg addManager(@RequestParam(value="managername")String managername,@RequestParam(value="password")String password,@RequestParam(value="roleid")List roleid) {
		int a=managerService.addManager(managername, password);
		Manager manager=managerService.selManager(managername);
		int b=managerRoleService.roleToManager(String.valueOf(manager.getId()), roleid);
		if (a==1) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}
	
	/**
	 * @author WYHY
	 * @param 删除管理员
	 * */
	@RequestMapping("/delMana")
	@ResponseBody
	public Msg delManager(@RequestParam(value="managerid")String managerid) {
		int a=managerService.delManagerF(managerid);
		if (a==1) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}
	
	/**
	 * @author WYHY
	 * @param 修改管理员
	 * */
	@RequestMapping("/updMana")
	@ResponseBody
	public Msg updManager(@RequestParam(value="managerid")String managerid,@RequestParam(value="managername")String managername,@RequestParam(value="password")String password,@RequestParam(value="roleid")List roleid) {
		int a=managerService.updManager(managerid, managername, password);
		int b=managerRoleService.roleToManager(managerid, roleid);
		if (a==1) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}
	
	/**
	 * @author WYHY
	 * @param 查询所有管理员
	 * */
	@RequestMapping("/selAllMana")
	@ResponseBody
	public Msg selAllManager(@RequestParam(value = "pn") Integer pn) {
		PageHelper.startPage(pn, 10);
		List<Manager> list=managerService.selAllManager();
		PageInfo<Manager> page = new PageInfo<Manager>(list,  5);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * @author WYHY
	 * @param 用户名查询管理员
	 * */
	@RequestMapping("/selMana")
	@ResponseBody
	public Manager selManager(@RequestParam(value="managername")String managername) {
		Manager manager=managerService.selManager(managername);
		return manager;
	}
	
	/**
	 * @author WYHY
	 * @param 查询管理员详细信息
	 * */
	@RequestMapping("/selManaA")
	@ResponseBody
	public Map selManagerA(@RequestParam(value="managerid")String managerid) {
		Manager manager=managerService.selManagerA(managerid);
		List list=managerRoleService.managerSelRole(managerid);
		Map map=new HashMap<>();
		map.put("manager", manager);
		map.put("roles", list);
		return map;
	}
	
	/**
	 * @author WYHY
	 * @param 角色表
	 * */
	/*@RequestMapping("/selRole")
	@ResponseBody
	public List selTree() {
		List list=roleService.selTree();
		return list;
	}*/
}
